<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of CreateReceivingOrder
 *
 * @author Administrator
 */
class CreateReceivingOrder extends CI_Controller
{
var $users;
    function __construct()
    {
        parent::__construct();
        $this->load->service('wms/receiving_order_service');
        $this->load->service('wms/api_service');
        $this->load->service('wms/sap_service');
        $this->users = $this->session->userdata('users');
                $this->lang->load('wms');
        if (empty($this->users['id']))
        {
            redirect(blang_url());
            return;
        }
    }

    function create_order($data)
    {
        $inserArray = array(
            'whs_code' => $data['whs_code'],
            'comments' => $data['comments'],
            'creator_id' => $data['creator_id'],
            'status' => 0,
            'order_from' => $data['order_from'],
            'ref_num' => $data['ref_num'],
            'lstProduct' => $data['lstProduct']
        );
        $orderId = $this->receiving_order_service->create_receiving_order_with_products_t($inserArray);
        return $orderId;
    }

    function get_zcsq($tranOrder)
    {
        $topics = $this->sap_service->getZCSQ($tranOrder);
        $success = false;
        $data = array();
        if ($topics)
        {
            $success = true;
            foreach ($topics as $key => $value)
            {
                $data[] = array(
                    'SKU' => $value['ItemCode'],
                    '目的仓' => $value['WhsCode2'],
                    '数量' => $value['Quantity'],
                    '转储申请单号' => $value['NumAtCard'],
                    '追踪号' => $value['ShipNum'],
                    '产品名称' => $value['ItemName'],
                );
            }
        }
        $totalCount = count($data);
        $obj = new stdClass ();
        $obj->success = $success;
        $obj->totalCount = $totalCount;
        $obj->topics = $data;
        header('Content-Type: text/html; charset=utf-8');
        $jsstring = json_encode($obj);
        echo $jsstring;
    }

    function create_byzc($tranOrder)
    {
        $merchant=FALSE;
        if(strstr($tranOrder, '_')){
        $merchant=TRUE;
        $merchant_data=explode("_", $tranOrder);
        $tranOrder=$merchant_data[0];
        }
        $topics = $this->sap_service->getZCSQ($tranOrder);
        if ($topics)
        {
            $lstProduct = array();
            $whs_code = '';
            $ref_num =$tranOrder;
            foreach ($topics as $key => $value)
            {
                $whs_code = $value['WhsCode2'];
                $lstProduct[] = array(
                    'sku' => $value['ItemCode'],
                    'quantity' => $value['Quantity'],
                );
            }
            if($merchant){
                $whs_code=$merchant_data[1];
            }
            $inserArray = array(
                'whs_code' => $whs_code,
                'comments' => 'from SAP',
                'order_from' => 'sap',
                'creator_id' => $this->users['id'],
                'ref_num' => $ref_num,
                'status' => 0,
                'lstProduct' => $lstProduct
            );
            $orderId = $this->receiving_order_service->create_receiving_order_with_products_t($inserArray);
            if ($orderId)
            {
                redirect(blang_url('wms/ReceivingOrderList/show_list?refNum=' . $tranOrder));
            }
            else
            {
                show_error(lang('wms_tips_failed'));
            }
        }
        else
        {
            show_error(lang('wms_tips_zbdzcsqd'));
        }
    }

}

?>
